package com.newproject.resmi;

import java.util.LinkedList;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class Money extends ListActivity{

	private ProgressDialog instr_ProgressDialog = null;
	private LinkedList<CurrencyItem> money = null;
	private CurrencyItemAdapter adapter;
	private Runnable viewMoney;
	private volatile Thread getMoneyThread;
	
	private static final String USER_DETAILS = "UserPrefs";
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_money);
		
		this.money = new LinkedList<CurrencyItem>();
		this.adapter = new CurrencyItemAdapter(this,
				R.layout.my_money_row, money);
		setListAdapter(this.adapter);
		
		viewMoney = new Runnable() {
			// @Override
			public void run() {
				getMoney();			
			}
		};		
	}
	
	@Override
    public void onPause() {    	
    	super.onPause(); 	
    }
    
    @Override
    public void onResume() {    	
    	super.onResume();
    	getMoneyThread = new Thread(null, viewMoney, "Get Money Thread");
    	getMoneyThread.start();
    	instr_ProgressDialog = ProgressDialog.show(Money.this,
				this.getString(R.string.wait),
				this.getString(R.string.loading), true);	
    }
	
	
	private Runnable returnRes = new Runnable() {
		// @Override
		public void run() {
			adapter.notifyDataSetChanged();
			instr_ProgressDialog.dismiss();
		}
	};
	
	private void getMoney() {
		SharedPreferences settings = getSharedPreferences(USER_DETAILS, Context.MODE_PRIVATE);;					
		String clientId = settings.getString("clientId", "error");		
		DataManager.fillMyMoney(money, clientId);
		runOnUiThread(returnRes);
	}
	
	private class CurrencyItemAdapter extends ArrayAdapter<CurrencyItem> {
		private LinkedList<CurrencyItem> items;

		public CurrencyItemAdapter(Context context, int textViewResourceId,
				LinkedList<CurrencyItem> items) {
			super(context, textViewResourceId, items);
			this.items = items;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
				v = vi.inflate(R.layout.my_money_row, null);
			}
			CurrencyItem o = items.get(position);
			if (o != null) {				
				TextView avail = (TextView) v.findViewById(R.id.avail);
				TextView blocked = (TextView) v.findViewById(R.id.blocked);
				TextView currency = (TextView) v.findViewById(R.id.currency);
				if (avail != null) {
					avail.setText(o.format(o.getFreeAmount()));
				}
				if (blocked != null) {
					blocked.setText(o.format(o.getBlockedAmount())); 
				}
				if (currency != null) {
					currency.setText(o.getTicker());				
				}
			}
			return v;
		}
	}
	  
}
